package org.scohen.juploadr.uploadapi.zooomr.authentication;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.prefs.Preferences;

import org.scohen.juploadr.uploadapi.Account;
import org.scohen.juploadr.uploadapi.CommunicationException;
import org.scohen.juploadr.uploadapi.ImageUploadApi;
import org.scohen.juploadr.uploadapi.ProtocolException;
import org.scohen.juploadr.uploadapi.zooomr.ZooomrRestApi;
import org.scohen.juploadr.uploadapi.zooomr.upload.GetUploadStatus;
import org.scohen.juploadr.uploadapi.zooomr.urls.GetUserPhotos;

public class ZooomrAccount extends Account {

    private String password;
    private String frob;
    private String token;
    private boolean pro;
    private static ZooomrAccount nullAccount = new ZooomrAccount();
    private static final Preferences preferences = Preferences.userNodeForPackage(ZooomrAccount.class);

    public static final ZooomrAccount getNullAccount() {
        nullAccount = new ZooomrAccount();
        return nullAccount;
    }

    public ZooomrAccount() {
        super("null-account"); //$NON-NLS-1$
        this.username = ""; //$NON-NLS-1$
        this.pro = false;
    }

    public ZooomrAccount(GetUploadStatus gus) {
        super(gus.getUsername());
        configureFrom(gus);
    }

    public ZooomrAccount(String name) {
        super("account=" + System.currentTimeMillis()); //$NON-NLS-1$
    }

    public void configureFrom(GetUploadStatus gus) {
        this.username = gus.getUsername();
        this.name = gus.getUsername();
        this.setCurrentUploadUsed(gus.getUsedBandwidth());
        this.setMonthlyUploadLimit(gus.getMaxBandwidth());
        this.setPro(gus.isPro());
        this.setUsername(gus.getUsername());
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public boolean isPro() {
        return pro;
    }

    /**
     * @param password
     *            The password to set.
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @param pro
     *            The pro to set.
     */
    public void setPro(boolean pro) {
        this.pro = pro;
    }

    /**
     * @param username
     *            The username to set.
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return Returns the frob.
     */
    public String getFrob() {
        return frob;
    }

    /**
     * @param frob
     *            The frob to set.
     */
    public void setFrob(String frob) {
        this.frob = frob;
    }

    /**
     * @return Returns the token.
     */
    public String getToken() {
        return token;
    }

    /**
     * @param token
     *            The token to set.
     */
    public void setToken(String token) {
        this.token = token;
    }

    public void save() {
        if (isAuthenticated() && getUsername() != null && getUsername().length() > 0) {
            super.save();
        }
    }

    public static Map<String, Account> allAccounts() {
        return allAccountsIn(ZooomrAccount.class);
    }

    public URL getPhotoURL() {
        URL photoURL = null;
        try {
            GetUserPhotos gup = new GetUserPhotos();
            if (gup.execute()) {
                photoURL = gup.getURL();
            }
        } catch (ProtocolException e) {

            e.printStackTrace();
        } catch (CommunicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (photoURL == null) {
                try {
                    photoURL = new URL("http://www.zooomr.com");
                } catch (MalformedURLException e) {
                    // this can't happen
                }
            }
        }
        return photoURL;
    }

    public ImageUploadApi getApi() {
        return ZooomrRestApi.getInstance();
    }

}
